﻿using System;

namespace Jimu
{
    /// <summary>
    ///     who has this attribute service will be register as microservice
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class JimuServiceAttribute : JimuServiceDescAttribute
    {
        public JimuServiceAttribute(string rest, string httpMethod)
        {
            this.Rest = rest;
            this.HttpMethod = httpMethod;
        }
        public JimuServiceAttribute()
        {
            IsWaitExecution = true;
        }

        /// <summary>
        ///     service id, if not set, default value will be generated by IServiceIdGenerator
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        ///     invoke path of the service
        /// </summary>
        public string RoutePath { get; set; }

        /// <summary>
        ///     sync to invoke
        /// </summary>
        public bool IsWaitExecution { get; set; }

        /// <summary>
        ///     creater for this service
        /// </summary>
        public string CreatedBy { get; set; }

        /// <summary>
        ///     all the created and modified comment for this service
        /// </summary>
        public string Comment { get; set; }

        /// <summary>
        ///     service create date
        /// </summary>
        public string CreatedDate { get; set; }

        /// <summary>
        ///    whether allow anonymous call, default false
        /// </summary>
        public bool AllowAnonymous { get; set; }

        /// <summary>
        ///     just for specify roles to invoke this service
        /// </summary>
        public string Roles { get; set; }

        /// <summary>
        ///     set the http method for http request
        /// </summary>
        public string HttpMethod { get; set; }

        /// <summary>
        /// the path for rest, e.g.: /users/{uid}/friends/{fid}
        /// </summary>
        public string Rest { get; set; }

        public override void Apply(JimuServiceDesc descriptor)
        {
            descriptor.WaitExecution = IsWaitExecution;
            descriptor.AllowAnonymous = AllowAnonymous;
            if (!string.IsNullOrEmpty(Id))
                descriptor.Id = Id;
            if (!string.IsNullOrEmpty(RoutePath))
                descriptor.RoutePath = RoutePath;
            if (!string.IsNullOrEmpty(CreatedBy))
                descriptor.CreatedBy = CreatedBy;
            if (!string.IsNullOrEmpty(CreatedDate))
                descriptor.CreatedDate = CreatedDate;
            if (!string.IsNullOrEmpty(Comment))
                descriptor.Comment = Comment;
            if (!string.IsNullOrEmpty(Roles))
                descriptor.Roles = Roles;
            if (!string.IsNullOrEmpty(HttpMethod))
                descriptor.HttpMethod = HttpMethod;
            if (!string.IsNullOrEmpty(Rest))
                descriptor.Rest = Rest;
        }
    }

    [AttributeUsage(AttributeTargets.Method)]
    public class JimuGetAttribute : JimuServiceAttribute
    {
        public JimuGetAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "GET";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuGetAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "GET";
            this.AllowAnonymous = allowAnonymous;

        }

    }

    [AttributeUsage(AttributeTargets.Method)]
    public class JimuPostAttribute : JimuServiceAttribute
    {
        public JimuPostAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "POST";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuPostAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "POST";
            this.AllowAnonymous = allowAnonymous;

        }

    }
    [AttributeUsage(AttributeTargets.Method)]
    public class JimuPutAttribute : JimuServiceAttribute
    {
        public JimuPutAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "PUT";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuPutAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "PUT";
            this.AllowAnonymous = allowAnonymous;

        }

    }

    [AttributeUsage(AttributeTargets.Method)]
    public class JimuDeleteAttribute : JimuServiceAttribute
    {
        public JimuDeleteAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "DELETE";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuDeleteAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "DELETE";
            this.AllowAnonymous = allowAnonymous;
        }

    }

    [AttributeUsage(AttributeTargets.Method)]
    public class JimuHeadAttribute : JimuServiceAttribute
    {
        public JimuHeadAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "HEAD";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuHeadAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "HEAD";
            this.AllowAnonymous = allowAnonymous;
        }
    }

    [AttributeUsage(AttributeTargets.Method)]
    public class JimuPatchAttribute : JimuServiceAttribute
    {
        public JimuPatchAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "PATCH";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuPatchAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "PATCH";
            this.AllowAnonymous = allowAnonymous;
        }
    }

    [AttributeUsage(AttributeTargets.Method)]
    public class JimuOptionsAttribute : JimuServiceAttribute
    {
        public JimuOptionsAttribute(string rest, bool allowAnonymous = false) : base()
        {
            this.Rest = rest;
            this.HttpMethod = "OPTIONS";
            this.AllowAnonymous = allowAnonymous;
        }

        public JimuOptionsAttribute(bool allowAnonymous = false) : base()
        {
            this.HttpMethod = "OPTIONS";
            this.AllowAnonymous = allowAnonymous;
        }
    }
}